
library(shiny)
library(ggplot2)
library(ggthemes)
library(wesanderson)

source("./base.R")

ui <- fluidPage(
  titlePanel("Stock Basic"),
  
  sidebarLayout(
    sidebarPanel(
      selectInput("usedb", h4("Data From"),
                  choices = c("MongoDB",
                                 "CSV"), 
                  selected = "MongoDB"),
      textInput("path", h4("Path of csv directory"), placeholder = "Only valid for CSV"),
      textInput("username", "Username", placeholder = "Username to mongodb"),
      textInput("password", "Password", placeholder = "Password to mongodb"),
      dateInput("date", h4("Choose Date"), value=Sys.Date()),
      sliderInput("highlow", "Limit", min = -11.0, max = 11.0, step = 0.1, value = c(9.5, 11.0)),
      selectInput("concept", h4("Concept:"), 
                  choices = c("All", "None"),
                  selected = "All"),
      selectInput("area", h4("Area"),
                  choices = c("All", "None"),
                  selected = "All"),
      selectInput("industry", h4("industry"),
                  choices = c("All", "None"),
                  selected = "All")
    ),
    
    mainPanel(
      plotOutput("summary"),
      plotOutput("classify_industry"),
      plotOutput("classify_area"),
      dataTableOutput("datalist")
    )
  )
)

old_concepts <- Nile
old_industry <- Nile
old_area <- Nile

get_hc_filter <- function(input, session) {
  USEDB = TRUE
  csv_path = ""
  if (input$usedb == "CSV") {
    USEDB = FALSE
    csv_path = input$path
  }
  time_str <- gsub("-","", input$date)
  paste("You selected ", USEDB, ", time ", time_str)
  
  todaydata <- get_data_todayall(USEDB, "", time_str, path = csv_path, user = input$username, pwd = input$password)
  todaydata <- todayall_add_level(todaydata)
  
  companies <- get_data_companies(USEDB, user = input$username, pwd = input$password)
  concepts <- get_data_concepts(USEDB, user = input$username, pwd = input$password)
  
  hc_all <- take_out_companies(todaydata, companies, input$highlow[2], input$highlow[1])
  
  conceptlist <- get_all_concepts_from(concepts, hc_all)
  industrylist <- get_all_industry(hc_all)
  arealist <- get_all_area(hc_all)

  updateSelectInput(session, "concept", choices = c("All", conceptlist), selected = input$concept)
  updateSelectInput(session, "area", choices = c("All", arealist), selected = input$area)
  updateSelectInput(session, "industry", choices = c("All", industrylist), selected = input$industry)
  
  hc_all <- get_data_filter(hc_all, concepts, input$concept, input$area, input$industry)
  return(hc_all)
}

server <- function(input, output, session) {

  output$summary <- renderPlot({
    USEDB = TRUE
    csv_path = ""
    if (input$usedb == "CSV") {
      USEDB = FALSE
      csv_path = input$path
    }
    time_str <- gsub("-","", input$date)
    paste("You selected ", USEDB, ", time ", time_str)
    
    todaydata <- get_data_todayall(USEDB, "", time_str, path=csv_path, user = input$username, pwd = input$password)
    todaydata <- todayall_add_level(todaydata)
    todayss <- todayall_parse_level(todaydata)
    draw_todayall_ss(todayss)
  })
  
  output$classify_industry <- renderPlot({
    USEDB = TRUE
    if (input$usedb == "CSV") {
      USEDB = FALSE
    }
    companies <- get_data_companies(USEDB, user = input$username, pwd = input$password)
    hc_all <- get_hc_filter(input, session)
    
    draw_classify(hc_all$industry)
  })
  
  output$classify_area <- renderPlot({
    USEDB = TRUE
    if (input$usedb == "CSV") {
      USEDB = FALSE
    }
    companies <- get_data_companies(USEDB, user = input$username, pwd = input$password)
    hc_all <- get_hc_filter(input, session)
    draw_classify(hc_all$area)
  })
 
  output$datalist <- renderDataTable({
    hc_all <- get_hc_filter(input, session)
    hc_all
  })
}

shinyApp(ui = ui, server = server)
